﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Geodatabase;
using System.Collections.ObjectModel;

namespace AgrinsaGeoDao
{
  public  class Utils
    {
        public Utils() { }

        public void IWorkspaceDomains_Example(IWorkspace workspace, string NombreDominio)
        {
            //The function shows how to create a new range domain, then add it to the workspace.
            IWorkspaceDomains workspaceDomains = workspace as IWorkspaceDomains;

            //Create a new range domain
            IRangeDomain rangeDomain = new RangeDomainClass();
            rangeDomain.MaxValue = 2000;
            rangeDomain.MinValue = 500;

            IDomain domain = rangeDomain as IDomain;
            domain.Name = "Capacity constraint";
            domain.FieldType = esriFieldType.esriFieldTypeDouble;
            domain.SplitPolicy = esriSplitPolicyType.esriSPTGeometryRatio;
            domain.MergePolicy = esriMergePolicyType.esriMPTSumValues;

            //Add the new domain to the workspace;
            workspaceDomains.AddDomain(domain);

            //Create a new coded value domain
            ICodedValueDomain codedValueDomain = new CodedValueDomainClass();
            codedValueDomain.AddCode("RES", "Residential");
            codedValueDomain.AddCode("COM", "Commercial");
            codedValueDomain.AddCode("IND", "Industrial");
            codedValueDomain.AddCode("BLD", "Building");

            IDomain domain2 = codedValueDomain as IDomain;
            domain2.Name = "Building types";
            domain2.FieldType = esriFieldType.esriFieldTypeString;
            domain2.SplitPolicy = esriSplitPolicyType.esriSPTDuplicate;
            domain2.MergePolicy = esriMergePolicyType.esriMPTDefaultValue;



            //Add the new domain to the workspace;
            workspaceDomains.AddDomain(domain2);

            //Get the domain that was just added and print out it's properties
            IDomain domain3 = workspaceDomains.get_DomainByName("Building types");
            Console.WriteLine("The name of the domain is: {0} and is of type: {1}", domain3.Name, domain3.Type);

            //IWorkspaceDomains::CanDeleteDomain and DeleteDomain
            IEnumDomain enumDomain = workspaceDomains.Domains;
            IDomain myDomain = enumDomain.Next();
            while (myDomain != null)
            {
                if (workspaceDomains.get_CanDeleteDomain(myDomain.Name.ToString()))
                {
                    //we can delete this domain
                    workspaceDomains.DeleteDomain(myDomain.Name.ToString());
                }
                myDomain = enumDomain.Next();
            }

        }

        public Collection<AgrinsaObjects.Dominio> getDomain(IWorkspace workspace, string NombreDominio)
        {
            Collection<AgrinsaObjects.Dominio> dominio = new Collection<AgrinsaObjects.Dominio>();
            AgrinsaObjects.Dominio elementoDominio;
            IWorkspaceDomains workspaceDomains = workspace as IWorkspaceDomains;
            //Get the domain that was just added and print out it's properties
            IDomain domain3 = workspaceDomains.get_DomainByName(NombreDominio);
            if (domain3.Type == esriDomainType.esriDTCodedValue)
            {
                ICodedValueDomain codedValueDomain = (ICodedValueDomain)domain3;
                for (int i = 0; i < codedValueDomain.CodeCount; i++)
                {
                    // codedValueDomain.get_Value(i) returns the code that is stored in the geodatabase (e.g, RES).
                    // codedValueDomain.get_Name(i) returns the name that is shown in the ArcMap inspector (e.g., Residential).
                    elementoDominio = new AgrinsaObjects.Dominio();
                    elementoDominio.Llave =codedValueDomain.get_Name(i);
                    elementoDominio.Valor =codedValueDomain.get_Value(i);
                    dominio.Add(elementoDominio);
                }
                return dominio;
            }
            else
            {
                IRangeDomain rangeDomain = (IRangeDomain)domain3;
                Console.WriteLine("Minimum Value: {0} Maximum Value: {1}",
                  rangeDomain.MinValue, rangeDomain.MaxValue);
                return null;
            }

            
        }
    }
}
